package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.characteristic.TrackCharacteristic;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.ITrackDetailService;
import com.xmy.cultivate.service.ITrackMarketService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.ConfigData;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/trackTeam")
public class TrackTeamController extends BaseController{

    private final Integer trackType = 2;

    @Resource
    TrackCharacteristic trackCharacteristic;

    @Autowired
    ITrackMarketService iTrackMarketService;

    @Autowired
    ITrackDetailService iTrackDetailService;

    @GetMapping()
    public Result findAll(@RequestParam Map<String,String> reMap){
        IPage<TrackMarket> iPage = trackCharacteristic.findAll(reMap, this.trackType);
//        List<TrackMarket> category = trackCharacteristic.getCategory(iPage.getRecords(), 0L);//无限树
//        iPage.setRecords(category);
        return new Result(Code.OK,new PageResult<>(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }


    @GetMapping(value = "/{id}",name = "course:findbyid")
    public Result findById(@PathVariable(value = "id") Long id){
        return trackCharacteristic.findById(id);
    }

    @PostMapping()
    public Result save(@RequestBody TrackMarket trackMarket){
        return trackCharacteristic.save(trackMarket, this.trackType);
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody TrackMarket trackMarket){
        return trackCharacteristic.update(trackMarket);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        return trackCharacteristic.delete(id);
    }

    @PostMapping("/addBatchSourse")
    public Result addBatchSourse(@RequestBody TrackDetail trackDetail){
        return trackCharacteristic.addBatchSourse(trackDetail);
    }

    @DeleteMapping("/deleteBatchSourse")
    public Result deleteBatchSourse(@RequestBody TrackMarket trackMarket){
        return trackCharacteristic.deleteBatchSourse(trackMarket);
    }


    /**
     *
     * @param isRepetition 是否允许重复
     */
    @GetMapping("checkTeacher")
    public Result checkTeacher(@RequestParam Map<String,String> reMap,
                               @RequestParam(value = "schoolIdList",defaultValue = "") String[] schoolIdList,
                               @RequestParam(value = "isRepetition",required = false) String isRepetition){
        return trackCharacteristic.checkTeacher(reMap, schoolIdList, isRepetition);
    }

    @GetMapping("checkSchool")
    public Result checkSchool(@RequestParam Map<String, Object> reMap){
        return trackCharacteristic.checkSchool(reMap);
    }


    @GetMapping("findAllCopy")
    public Result findAllCopy(@RequestParam Map<String,String> reMap){
        reMap.put("pageNum", "-1");
        reMap.put("pageSize", "-1");
        String source = reMap.get("source");//源
        String target = reMap.get("target");//目标
        reMap.put("classify", source);
        IPage<TrackMarket> iPage = trackCharacteristic.findAll(reMap, this.trackType);
        if (!iPage.getRecords().isEmpty()) {
            for (TrackMarket trackMarket : iPage.getRecords()) {
                trackMarket.setTrackDetailList(trackDetailList(trackMarket.getId()));
            }
        }
        List<TrackMarket> trackMarketList = trackCharacteristic.getCategory(iPage.getRecords(), 0L);//无限树
        Long pid = 0L;
        trackMarketSaveOrDetailSave(target, trackMarketList, pid);
        iPage.setRecords(trackMarketList);
        return new Result(Code.OK,new PageResult<>(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }

    private void categoryFor(List<TrackMarket> trackMarketList, String target, Long pid){
        trackMarketSaveOrDetailSave(target, trackMarketList, pid);
    }

    private void trackMarketSaveOrDetailSave(String target, List<TrackMarket> trackMarketList, Long pid) {
        if (!trackMarketList.isEmpty()) {
            for (TrackMarket trackMarket : trackMarketList) {
                TrackMarket one = iTrackMarketService.getOne(
                        new QueryWrapper<TrackMarket>()
                                .eq("team_name", trackMarket.getTeamName())
                                .eq("track_type", this.trackType)
                                .eq("classify", target)
                                .eq("year_part", trackMarket.getYearPart())
                                .eq("quarter_num", trackMarket.getQuarterNum())
                                .eq("deleted", 0)
                );
                if (one == null) {
                    TrackMarket trackMarketSave = new TrackMarket();
                    trackMarketSave.setTrackType(this.trackType);
                    trackMarketSave.setName(trackMarket.getName());
                    trackMarketSave.setClassify(Integer.valueOf(target));
                    trackMarketSave.setQuarterNum(trackMarket.getQuarterNum());
                    trackMarketSave.setPid(pid);
                    trackMarketSave.setYearPart(trackMarket.getYearPart());
                    trackMarketSave.setType(trackMarket.getType());
                    trackMarketSave.setTeamName(trackMarket.getTeamName());
                    trackMarketSave.setTeacherId(trackMarket.getTeacherId());
                    trackMarketSave.setTreeLevel(trackMarket.getTreeLevel());
                    iTrackMarketService.save(trackMarketSave);
                    one = iTrackMarketService.getById(trackMarketSave.getId());
                }
                categoryFor(trackMarket.getChildren(), target, one.getId());
                if (!trackMarket.getTrackDetailList().isEmpty()) {
                    for (TrackDetail trackDetail : trackMarket.getTrackDetailList()) {
                        trackDetail.setId(null);
                        trackDetail.setTrackId(one.getId());
                        trackDetail.setClassify(Integer.valueOf(target));
                        long count = iTrackDetailService.count(new QueryWrapper<TrackDetail>().eq("track_id", one.getId()).eq("sourse_id", trackDetail.getSourseId()));
                        if(count == 0L){
                            iTrackDetailService.save(trackDetail);
                        }
                    }
                }
            }
        }
    }

    //右边列表
    public List<TrackDetail> trackDetailList(Long trackId){
        QueryWrapper<TrackDetail> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("track_id",trackId);
        return iTrackDetailService.list(queryWrapper);
    }
}
